package cn.kgc.javaweb.servlet;

import cn.kgc.javaweb.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @author: mosin
 * @version: v1.0  2022/5/23
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("登录请求被接收");

        // 如何解决前端数据 中文乱码  设置 req 的编码格式    一定要放在获取请求参数之前
        req.setCharacterEncoding("utf-8");
       //获取前端请求参数  username  password
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");

        User user = User.builder().username(username).password(pwd).build();
        System.out.println("user = " + user);

        // TODO 将user对象传入 userService  进行登录业务的处理

        // 响应给浏览器  设置响应格式  html
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if("admin".equals(username)&&"888888".equals(pwd)){
            //登录成功
            // 通过resp 获取输出流 将响应数据响应给前端浏览器
            writer.write("<h1>恭喜你,登录成功</h1>");
        }else{
            writer.write("<h1>用户名或密码不正确，登录失败!</h1>");
        }
        writer.close();
    }
}
